import { StoreOptions } from "vuex";
import ACCESS_ENUM from "@/access/accessEnum";
import { UserControllerService } from "../../../generated";

export default {
  namespaced: true,
  state: () => {
    return {
      loginUser: {
        userName: "未登录",
      },
    };
  },
  mutations: {
    updateUser(state, payload) {
      state.loginUser = payload;
    },
  },
  actions: {
    async getLoginUser({ commit, state }, payload) {
      // 从远程请求获取登录信息
      const res = await UserControllerService.getLoginUserUsingGet();
      // console.log(res);
      if (res.code === 0) {
        // console.log("设置userRole为具体值");
        // res.data = { ...res.data, userName: "张三" };
        commit("updateUser", res.data);
      } else {
        console.log("设置userRole为NOT_LOGIN");
        commit("updateUser", {
          ...state.loginUser,
          userRole: ACCESS_ENUM.NOT_LOGIN,
        });
      }
      // console.log("action函数执行完毕", state.loginUser);
    },
  },
} as StoreOptions<any>;
